//
//  SEGVisitorAccessHouseViewController.m
//  SEGPropertyBaseService
//
//  Created by 乔杨 on 2020/5/8.
//

#import "SEGVisitorAccessHouseViewController.h"

#import "SEGVisitorAccessNumberViewCell.h"

@interface SEGVisitorAccessHouseViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) NSArray<SEGProfileHouselistModel *> *houseListArr;

@end

@implementation SEGVisitorAccessHouseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showCustomNavigationBackButton];
    self.view.backgroundColor = kColor_bg;
    self.title = @"选择房间号";

    self.houseListArr = [[SEGProfileManager sharedManager].profile.houseList copy];
    [self.view addSubview:self.tableView];
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.houseListArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGVisitorAccessNumberViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVisitorAccessNumberViewCellIdentifier forIndexPath:indexPath];
    SEGProfileHouselistModel *houseModel = self.houseListArr[indexPath.row];
    BOOL isSelected = self.houseId.integerValue == houseModel.houseId.integerValue ? YES:NO;
    [cell updateDoorCellWithNumber:houseModel.houseName isSelected:isSelected];
    
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SEGProfileHouselistModel *houseModel = self.houseListArr[indexPath.row];
    self.houseId = houseModel.houseId;
    [tableView reloadData];
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedHouseName:houseId:)]) {
        [self.delegate selectedHouseName:houseModel.houseName houseId:self.houseId];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UIEdgeInsets insets = UIEdgeInsetsZero;//屏幕等宽分割线
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:insets];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:insets];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:insets];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:insets];
    }
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        [_tableView registerClass:[SEGVisitorAccessNumberViewCell class] forCellReuseIdentifier:kVisitorAccessNumberViewCellIdentifier];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end
